using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class CustomerloanService : DataFactory, CustomerloanIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerloanEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfoCustomerNeedRecordId(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerloanEntity>(m => m.CustomerNeedRecordId == id);
            }
        }


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfoByUUId(string uuid)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerloanEntity>(m => m.UUID == uuid);
            }
        }


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<List<CustomerloanEntity>> GetInfoByUUId(List<string> uuids)
        {
            using (var db = this.Basic)
            {
                return await db.GetListAsync<CustomerloanEntity>(m => uuids.Contains(m.UUID));
            }
        }


        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerloanEntity>> GetPageList(CustomerloanListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerloanEntity>();
                var queryParam = input.queryJson.ToObject();
                //查询贷款金额
                if (input.loanAmount.ToInt() != 0)
                {
                    //queryWhere = queryWhere.And(m => m.LoanAmount == input.loanAmount);
                }

                if (input.customerId.HasValue)
                {
                    // queryWhere = queryWhere.And(m => m.CustomerId == input.customerId.Value);
                }
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerloanEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerloanEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomerloanEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerloanEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomerloanEntity>> GetList(CustomerloanListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerloanEntity>();
                //查询贷款金额
                if (input.loanAmount.ToInt() != 0)
                {
                    //	queryWhere = queryWhere.And(m => m.LoanAmount == input.loanAmount);
                }

                if (input.customerNeedRecordId.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.CustomerNeedRecordId == input.customerNeedRecordId);
                }
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerloanEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}